<template>
  <div class="app-container">

    <el-breadcrumb>
      <el-breadcrumb-item :to="{path:'/index'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人资料</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <div class="avatar" title="点击更换头像">
      <el-upload
          class="avatar-uploader"
          :action="'/upload'"
          :show-file-list="false"
          :headers="{'token': 'xx'}"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
        <img v-if="form.avatar" :src="form.avatar" class="avatar" alt="">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </div>
    <br>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="form.realName"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="cellphone">
        <el-input v-model="form.cellphone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="update('form')">修改</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        id: undefined,
        realName: 'dandan',
        cellphone: '12345678901',
        email: 'dandan@vue.js',
        avatar: '/images/avatar01.png'
      },
      rules: {
        realName: [
          {required: true, message: '名称不能为空', trigger: 'change'},
          {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'change'}
        ],
        cellphone: [
          {pattern: /^1[3456789]\d{9}$/, message: "请输入正确的手机号", trigger: 'change'}
        ],
        email: [
          {
            pattern: /^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/,
            message: "请输入正确的邮箱",
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isImage = file.type.indexOf('image/') !== -1;
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isImage) {
        this.$message.error('只能上传图片');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过2MB');
      }
      return isImage && isLt2M;
    },
    handleAvatarSuccess(res) {
      this.changeIcon(res.data)
    },
    changeIcon(icon) {
      console.log(icon)
      // TODO 发送异步请求上传头像 ...
      this.$message({
        type: 'success',
        message: '头像上传成功'
      })
    },
    update(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.$message({
            type: 'error',
            message: '请正确填写参数'
          })
          return false
        }
        this.$message({
          type: 'success',
          message: '信息修改成功'
        })
      })
    },
    reset() {
      this.$refs['form'].resetFields()
    }
  },
  created() {
  }
}
</script>

<style scoped>
.avatar, .avatar-uploader {
  width: 80px;
  height: 80px;
  margin: 0 auto;
}

.avatar-uploader i {
  line-height: 80px;
  text-align: center;
  margin: 0 32px;
}

.avatar-uploader {
  border: 1px dashed #d9d9d9;
}

.avatar-uploader:hover {
  border-color: #409EFF;
}
</style>